function collatz(n) {
    let tot = 1;
    while (n > 1) {
        ++tot;
        // console.log(n+"->");
        if (n % 2 === 1) {
            n = n * 3 + 1;
        } else {
            n /= 2;
        }
    }
    return tot;
}

function longestCollatzSequence(limit) {
    let best = 1;
    for (let i = 2; i <= limit; i++) {
        if (collatz(i) > collatz(best)) {
            best = i;
        }
    }
    return best;

}

console.log(longestCollatzSequence(14));